//============================================================================
// Name        : graphics.cpp
// Author      : dfoelber
//============================================================================

#include "graphics/graphics.h"
#include <iostream>

using namespace std;

void Graphics::DrawRectangle( Surface* surface, Color c, int x, int y, int width, int height, bool fill )
{
	bool locked = SDL_MUSTLOCK(surface->GetSurface());
	// Lock the screen, if necessary
    if( locked ) 
    {
        if( SDL_LockSurface(surface->GetSurface()) < 0 ) 
        {
            return;
        }
    }
    
    Uint8 r = Uint8(c.r); Uint8 g = Uint8(c.g); Uint8 b = Uint8(c.b);
    Uint32 color = SDL_MapRGB(surface->GetSurface()->format, r, g, b);
    Uint32* bufp;
    
    do
    {
	    for( int i = x; i < x + width; i++ )
	    {
	    	// top and bottom
	        bufp = (Uint32*)surface->GetSurface()->pixels + (y * surface->GetSurface()->pitch / 4) + i;
	        *bufp = color;
	        bufp = (Uint32*)surface->GetSurface()->pixels + ((y + height - 1) * surface->GetSurface()->pitch / 4) + i;
	        *bufp = color;
	    }
	    
	    for( int i = y; i < y + height; i++ )
	    {
	    	// left and right#include "SDL_font.h"
	        bufp = (Uint32*)surface->GetSurface()->pixels + (i * surface->GetSurface()->pitch / 4) + x;
	        *bufp = color;
	        bufp = (Uint32*)surface->GetSurface()->pixels + (i * surface->GetSurface()->pitch / 4) + (x + width - 1);
	        *bufp = color;
	    }
	    
	    x++;
	    y++;
	    width -= 2;
	    if (width < 1)
	    	width = 1;
	    height -= 2;
	    if (height < 1)
	    	height = 1;
    }
    while( fill && (width > 1 || height > 1) );
    
    if( locked ) 
    {
        SDL_UnlockSurface(surface->GetSurface());
    }
}

void Graphics::DrawFont( Surface* surface, string s, Font font, int size, Color c, int x, int y, int alpha )
{
	TTF_Font* f = GetFont(font, size);
	
	if (f != NULL)
	{
		SDL_Color color = {c.r,c.g,c.b};
		SDL_Surface* fontSurface = TTF_RenderText_Solid( f, s.c_str(), color );
		
		SDL_SetAlpha( fontSurface, SDL_SRCALPHA, alpha );
		
		Surface* temp = new Surface( fontSurface );
		temp->Apply( surface, x, y );
		
		delete temp;
		
		TTF_CloseFont( f );
	}
}

void Graphics::DrawFont( Surface* surface, string s, Font font, int size, Color c, int x, int y, int width, int height, int alignment, int alpha )
{
	TTF_Font* f = GetFont(font, size);
	
	int textWidth = 0, textHeight = 0;
	
	if (f != NULL && TTF_SizeText(f, s.c_str(), &textWidth, &textHeight) == 0)
	{
		if ((int)(alignment & ALIGNMENT_MIDDLE) > 0)
		{
			x += (width / 2) - (textWidth / 2);
		}
		else if ((int)(alignment & ALIGNMENT_RIGHT) > 0)
		{
			x += width - textWidth;
		}
		if ((int)(alignment & ALIGNMENT_CENTER) > 0)
		{
			y += (height / 2) - (textHeight / 2);
		}
		else if ((int)(alignment & ALIGNMENT_BOTTOM) > 0)
		{
			y += height - textHeight;
		}
		
		SDL_Color color = {c.r,c.g,c.b};
		SDL_Surface* fontSurface = TTF_RenderText_Solid( f, s.c_str(), color );
		
		SDL_SetAlpha( fontSurface, SDL_SRCALPHA, alpha );
		
		Surface* temp = new Surface( fontSurface );
		temp->Apply( surface, x, y );
		
		delete temp;
		
		TTF_CloseFont( f );
	}
}

void Graphics::GetFontSize( Font font, string s, int size, int* w, int* h )
{
	TTF_Font* f = GetFont( font, size );
	
	if (f)
		TTF_SizeText(f, s.c_str(), w, h );
}
	
TTF_Font* Graphics::GetFont( Font font, int size )
{
	string directory;
	if( LINUX_FILESYSTEM )
		directory += "/usr/share/xyne/";
	
	string filename;
	switch (font)
	{
	case FONT_KNIGHT:
		filename = directory + "knight.ttf";
	case FONT_CICLE_FINA:
		filename = directory + "cicle_fina.ttf";
	case FONT_DEJA_VU_SANS:
		filename = directory + "DejaVuSans.ttf";
	case FONT_DEJA_VU_SANS_BOLD:
		filename = directory + "DejaVuSans-Bold.ttf";
	}
	
	return TTF_OpenFont( filename.c_str(), size );
}
